package action;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Comparator;
import java.util.List;

import comparetor.CustomerComparetorByBirth;
import comparetor.CustomerComparetorById;
import pojo.Customer;
import printer.impl.DataPrinterImpl;
import processor.DataProcessor;
import processor.impl.DataProcessorImpl;
import scanner.DataScanner;
import scanner.impl.DataScannerImpl;

public class DataProcessAction {
	private Comparator<Customer> comparator;
	private DataProcessor dp;
	private DataScanner scanner;
	private String format;
	
	public void action() throws Exception{
		scanner=new DataScannerImpl(new FileInputStream("data.txt"));
		List<Customer> list=scanner.scann();
		
		ServerSocket serverS=new ServerSocket(8889);
		Socket acceptS=serverS.accept();  //open port action
		System.out.println("connection............");
		OutputStream os=acceptS.getOutputStream();
		InputStream is=acceptS.getInputStream();
		BufferedReader bufR=new BufferedReader(new InputStreamReader(is));
		BufferedWriter bufW=new BufferedWriter(new OutputStreamWriter(os));
	    bufW.write(DataPrinterImpl.printHelp().toString());
	    bufW.write("\n\rPlease input your command......\n\r");
	    bufW.flush();
		String command="";
		while((command=bufR.readLine())!=null)
		{
			System.out.println(command);
			String[] strCommand=command.split(" ");
			if(strCommand[0].equals("C"))
			{
				if(strCommand[1].equals("ID")){
		    		format="XML";
		    		comparator=new CustomerComparetorById();
		    		dp=new DataProcessorImpl(list, format);
		    		StringBuilder sb=dp.process(comparator);
		    		bufW.write(sb.toString());
					bufW.flush();
		    	}
				else if(strCommand[1].equals("BIRTH")){
		    		format="XML";
		    		comparator=new CustomerComparetorByBirth();
		    		dp=new DataProcessorImpl(list, format);
		    		StringBuilder sb=dp.process(comparator);
		    		bufW.write(sb.toString());
					bufW.flush();
		    	}
				else
				{
					format="HELP";
		    		comparator=new CustomerComparetorByBirth();
		    		dp=new DataProcessorImpl(list, format);
		    		StringBuilder sb=dp.process(comparator);
		    		bufW.write(sb.toString());
					bufW.flush();
				}
					
			}
			else if(strCommand[0].equals("L"))
		    {
		    	if(strCommand[1].equals("XML")){
		    		format="XML";
		    		comparator=new CustomerComparetorByBirth();
		    		dp=new DataProcessorImpl(list, format);
		    		StringBuilder sb=dp.process(comparator);
		    		bufW.write(sb.toString());
					bufW.flush();
		    	}
		    	else if(strCommand[1].equals("ID")){
		    		format="I/B";
		    		comparator=new CustomerComparetorById();
		    		dp=new DataProcessorImpl(list, format);
		    		StringBuilder sb=dp.process(comparator);
		    		bufW.write(sb.toString());
					bufW.flush();
		    	}
		    	else if(strCommand[1].equals("BIRTH")){
		    		format="I/B";
		    		comparator=new CustomerComparetorByBirth();
		    		dp=new DataProcessorImpl(list, format);
		    		StringBuilder sb=dp.process(comparator);
		    		bufW.write(sb.toString());
					bufW.flush();
		    	}
		    	else 
		    	{
		    		format="HELP";
		    		comparator=new CustomerComparetorByBirth();
		    		dp=new DataProcessorImpl(list, format);
		    		StringBuilder sb=dp.process(comparator);
		    		bufW.write(sb.toString());
					bufW.flush();
		    	}
		    }
			else if(strCommand[0].equals("Q"))
			{
				bufW.close();
				bufR.close();
				System.exit(0);
			}
		    else
		    {
		    	format="HELP";
	    		comparator=new CustomerComparetorByBirth();
	    		dp=new DataProcessorImpl(list, format);
	    		StringBuilder sb=dp.process(comparator);
	    		bufW.write(sb.toString());
				bufW.flush();
		    }
			bufW.write("\n\rPlease input your command......\n\r");
			bufW.flush();
			
		}
		
		
	}
	
	
	public static void main(String[] args) throws Exception{
		new DataProcessAction().action();
	}
}
